package business.manage.dictionary;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import common.database.bean.Page;
import common.util.UUIDGenerator;

import business.manage.user.dto.User;

@Controller
@RequestMapping("dict")
public class DictionaryController {
	Logger logger = Logger.getLogger(this.getClass());

	@Autowired
	private DictionaryService dictionaryService;
	@Autowired
	private StaticDictionary staticDictionary;

	@RequestMapping("toDictionary")
	public String toDictionary(Model model) {
		model.addAttribute("listPage", "/dict/list");
		model.addAttribute("operatePage", "/dict/toAddDictionary");
		return "default";
	}

	@RequestMapping("/list")
	public String listDictionary(Model model, @ModelAttribute DictArgument da) {
		Page page = new Page(da.getCurrentPage());
		try {
			page = dictionaryService.listDictionary(da, page);
			List<Dictionary> list = dictionaryService.listAllPid();
			model.addAttribute("parentDictList", list);
			model.addAttribute("da", da);
			model.addAttribute("page", page);
			return "/dictionary/listDict";
		} catch (Exception e) {
			// TODO: handle exception
			logger.error(e.getMessage(),e);
			return "error";
		}
		
	}

	@RequestMapping("/deleteDictionary/{ids}")
	public String deleteDictionary(Model model, @PathVariable String ids) {
		try {
			if (ids == null || ids.equals("")) {
				throw new Exception("DELTE ID MUST NOT NULL");
			}
			String[] idArray = ids.split(",");

			dictionaryService.deleteByArrayId(idArray);
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
		model.addAttribute("refreshParent", "true");
		return "success";
	}

	@RequestMapping("/toAddDictionary")
	public String toAddDictionary(Model model, @ModelAttribute DictArgument da) {
		try {
			List<Dictionary> list = dictionaryService.listAllPid();
			model.addAttribute("parentDictList", list);
			return "/dictionary/operDict";
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return "error";
		}
		
	}
	
	@RequestMapping("/toUpdateDictionary/{id}")
	public String toUpdateDictionary(Model model, @PathVariable String id) {
		try {
			Dictionary dictionary = dictionaryService.getDictionaryById(id);
			List<Dictionary> list = dictionaryService.listAllPid();
			model.addAttribute("parentDictList", list);
			model.addAttribute("dict", dictionary);
			return "/dictionary/operDict";
		} catch (Exception e) {
			// TODO: handle exception
			logger.error(e.getMessage(),e);
			return "error";
		}
		
	}
	
	@RequestMapping("/updateDictionary")
	public String updateDictionary(HttpSession session, Model model,
			@ModelAttribute Dictionary dict) {
		User user = (User) session.getAttribute("user");
		
		try {
			Dictionary dictionary = dictionaryService.getDictionaryById(dict.getId());

			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			String dateNow = sf.format(new Date());
			dict.setCreateBy(dictionary.getCreateBy());
			dict.setCreateDate(dictionary.getCreateDate());
			dict.setUpdateBy(user.getId());
			dict.setUpdateDate(dateNow);
			dictionaryService.update(dict);
			model.addAttribute("message", "操作成功");
			return toUpdateDictionary(model, dict.getId());
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			return "error";
		}
		
	}

	@RequestMapping("/addDictionary")
	public String addDictionary(HttpSession session, Model model,
			@ModelAttribute Dictionary dict) {
		User user = (User) session.getAttribute("user");
		dict.setCreateBy(user.getId());
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		String dateNow = sf.format(new Date());
		dict.setCreateDate(dateNow);
		dict.setUpdateBy(user.getId());
		dict.setUpdateDate(dateNow);
		dict.setId(UUIDGenerator.getUUID());
		try {
			dictionaryService.insert(dict);
			model.addAttribute("message", "操作成功");
			return toAddDictionary(model, null);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			return "error";
		}
		
	}

	@RequestMapping("/refreshCache")
	public String refreshCache() {
		try {
			staticDictionary.initDictMap();
			return "success";
		} catch (Exception e) {
			// TODO: handle exception
			logger.error(e.getMessage(),e);
			return "error";
		}
		
	}
}
